package com.itestor.iptv.modules.common.actions;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.itestor.iptv.exceptions.ApplicationException;
import com.itestor.iptv.modules.common.CommonConstant;
import com.itestor.iptv.modules.common.service.RobotConfigService;

/**
 * 
 * @author masaki
 * 
 */
public class LoginAction extends Action {

    /**
     * 业务对象
     */
    private transient RobotConfigService robotConfigService;

    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionFroward
     * @throws Exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map<String, String> userMap = robotConfigService.getLoginUser(username,
                password);

        if (null != userMap) {
            request.getSession().setAttribute("ROBOT_USER", userMap);

        } else {

            request.setAttribute("username", username);
            request.setAttribute("password", password);

            throw new ApplicationException("robot.user.login.failue",
                    CommonConstant.PROPERTIES_BUNDLE);

            // return mapping.findForward("failure");
        }

        return mapping.findForward("success");

    }

    /**
     * 
     * @param pRobotConfigService
     */
    public void setRobotConfigService(RobotConfigService pRobotConfigService) {
        robotConfigService = pRobotConfigService;
    }

}
